package com.itljm.cxrMarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itljm.cxrMarket.entity.SysUserRole;
import com.itljm.cxrMarket.service.SysUserRoleService;
import com.itljm.cxrMarket.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService{
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    public void assignRolesToUser(Long userId, List<Long> roleIds) {
        // 清除现有角色关联
        sysUserRoleMapper.deleteByUserId(userId);

        // 插入新的角色关联
        for (Long roleId : roleIds) {
            sysUserRoleMapper.insertUserRole(userId, roleId);
        }
    }

    public List<String> getUserRoles(Long userId) {
        return sysUserRoleMapper.selectRoleNamesByUserId(userId);
    }
}




